<script lang="ts">
export default {
  name: 'MirrorImage'
}
</script>
<script lang="ts" setup>
import {ref, computed} from 'vue'
import {useMouseInElement} from '@vueuse/core'

const props = defineProps<{
  images: string[]
}>()
// 当前显示的图片索引
let active = ref(0)
// ref 获取 DOM 元素的位置
const target = ref(null)
// isisOutside为 true 的时候代表鼠标未进入目标元素，为 false 时代表鼠标进入目标元素
const {elementX, elementY, isOutside} = useMouseInElement(target)

const currentImgUrl  = computed(() => `url(${props.images[active.value]})`)

// 遮罩半透明图在商品大图中的坐标位置
const position = computed(() => {
  let x = elementX.value - 100
  let y = elementY.value - 100
  if (x <= 0) x = 0
  if (x >= 200) x = 200
  if (y <= 0) y = 0
  if (y >= 200) y = 200
  return {x, y}
})

const smallRef = ref();
const rightRef = ref();
const scrollStep = ref(68);
const scrollWidth = ref(0);
let flag = true;
function transformRight(){
  let offsetWidth = smallRef.value.offsetWidth + 68;
  if (Math.abs(scrollWidth.value)<offsetWidth){
    scrollWidth.value -= scrollStep.value + 5;
  }else {
    if (flag){
      //此处处理滑动到最后还有20px没有滑动直接判断一下
      scrollWidth.value -= 20;
      flag = false;
    }
  }
}
function transformLeft(){
  if (scrollWidth.value<0){
    //重置一下flag
    if (!flag){
      scrollWidth.value += 20;
      flag = true;
    }
    scrollWidth.value += scrollStep.value + 5;
  }
}


function onMouse(index:any){
  console.log(index)
  active.value = index
}
</script>
<template>
  <div>
    <div class="goods-image border">
      <!-- 放大镜的大盒子 -->
      <div
          :key="active"
          class="large"
          :style="[
        {
          backgroundImage: currentImgUrl,
          backgroundPosition: `-${position.x * 3}px -${position.y * 3}px`
        }
      ]"
          v-show="!isOutside"
      ></div>
      <div ref="target" class="middle">
        <img class="img-cover" :src="images[active]" alt="" />
        <!-- 鼠标移动时的遮罩层 -->
        <div
            class="layer"
            v-show="!isOutside"
            :style="{ left: `${position.x}px`, top: `${position.y}px` }"
        ></div>
      </div>
    </div>
    <div class="hidden-scroll m-t-10">
      <div class="small-box  d-flex">
        <span class="switch switch-left" @click="transformLeft"><left-outlined/></span>
        <div class="small-scroll">
          <ul ref="smallRef" :style="{transform: `translateX(${scrollWidth}px)`}" class="small">
            <li
                v-for="(item, index) in images"
                :key="item"
                :class="{ active: index === active }"
                :style="{marginLeft:index === 0?'5px': ''}"
                @mouseenter="onMouse(index)"
            >
              <img class="img-cover" :src="item" alt=""/>
            </li>
          </ul>
        </div>
        <span ref="rightRef" class="switch switch-right" @click="transformRight"><right-outlined/></span>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-image {
  width: 420px;
  height: 420px;
  position: relative;
  display: flex;
  align-items: center;
  z-index: 1;
  transition: 1s;
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 600px;
    height: 600px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 200% 200%;
    background-color: #f8f8f8;
  }

  .middle {
    margin: 0 auto;
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    position: relative;
    cursor: move;

    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }

}

ul, li {
  padding: 0;
  margin: 0;
}


.hidden-scroll{
  user-select: none;
  height: 70px;
  overflow: hidden;
}

.small-box {
  width: 420px;
  transform: translateY(-5px);
}


.small-scroll{
  transition: all .5s;
  width: 350px;
  overflow-x: auto;
}

.small {
  box-sizing: border-box;
  position: relative;
  display: flex;
  flex-flow: row nowrap;
  height: 80px;
  align-items: center;
  transition: all .5s;
  li {
    flex: 0 0  auto;
    width: 68px;
    height: 68px;
    display: inline-block;
    margin-right: 5px;
    cursor: pointer;
    z-index: 10;
    &:hover,
    &.active {
      border: 2px solid @theme-color;
    }
  }

}

.switch {
  display: inline-block;
  width: 35px;
  line-height: 80px;
  height: 80px;
  background-color: @theme-color;
  font-size: 2rem;
  color: #fff;
  cursor: pointer;
  z-index: 1;
}


</style>
